/**
 *
 * Copyright Pact Lab of H.I.T.
 *
 * Designed and Implemented by Grid Researching Group, 
 * Pact Lab, Harbin
 * 
 * This Project is part of the national 973 Project:
 * Internet Based Virtual Computing Environment
 *
 * http://pact518.hit.edu.cn
 * 
 * Author:       Meteor <meteorlxk@gmail.com> 
 * Copyright:    pact518 
 * Version:      1.0
 * Created:      2009-4-6 
 * LastModified: 2009-4-6
 */
package edu.hit.pact.pgse.util;

import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;



/**
 * @author Meteor
 *
 */
public class Globals {
	private static final String LOG4J_PROPERTIES_CFG = "conf/log4j.properties";
	
	public static final String SUPPORTED_FILE_PATH = "conf/supported.file.type.list";
	
	public static final String UNSUPPORTED_FILE_PATH = "conf/unsupported.file.type.list";
	
	private static Properties confProperties = Utilities.loadProperties("conf/conf.properties");
	
	public static void readLog4jProperties() {
		Properties p = Utilities.loadProperties(LOG4J_PROPERTIES_CFG);
		PropertyConfigurator.configure(p);
	}

	/*
	 * configurations for master
	 */
	public static final int CONSISTENT_HASH_NUMBER_OF_REPLICAS = 
		Integer.parseInt(confProperties.getProperty("consistent.hash.number.of.replicas", "200").trim());
	
	public static final int ALIVE_CHECKER_PROCRESSOR_INTERVAL = 
		Integer.parseInt(confProperties.getProperty("alive.checker.processor.interval", "300").trim());// 300 seconds
	
	public static final String MASTER_ADDRESS_IP = 
		confProperties.getProperty("master.address.ip", "127.0.0.1").trim();
	
	public static final int MASTER_LISTEN_PORT_FOR_CRAWLER = 
		Integer.parseInt(confProperties.getProperty("master.listen.port.for.crawler", "52602").trim());// 300 seconds
	
	public static final int MASTER_LISTEN_PORT_FOR_CLIENT = 
		Integer.parseInt(confProperties.getProperty("master.listen.port.for.client", "52601").trim());// 60 seconds
	
	public static final boolean MASTER_GRAPH_OPEN = 
		Boolean.parseBoolean(confProperties.getProperty("master.graph.open", "false").trim());
	
	/*
	 * configurations for directory server
	 */
	public static final String DIRECTORY_ADDRESS_IP = 
		confProperties.getProperty("directory.address.ip", "127.0.0.1").trim();
	
	public static final int DIRECTORY_ADDRESS_PORT = 
		Integer.parseInt(confProperties.getProperty("directory.address.port", "300").trim());// 300 seconds
	
	/*
	 * configurations for proxy
	 */
	public static final boolean CRAWLER_USE_PROXY = 
		Boolean.parseBoolean(confProperties.getProperty("crawler.host.use.http.proxy", "false").trim());

	public static final String CRAWLER_PROXY_IP = 
		confProperties.getProperty("crawler.host.proxy.ip", "127.0.0.1").trim();
	
	public static final int CRAWLER_PROXY_PORT = 
		Integer.parseInt(confProperties.getProperty("crawler.host.proxy.port", "3128").trim());
	
	/*
	 * configurations for crawler
	 */
	public static final int CRAWLER_DEFAULT_MAX_PERHOST_CONNECTIONS = 
		Integer.parseInt(confProperties.getProperty("crawler.default.max.perhost.connections", "2").trim());
	
	public static final int CRAWLER_DEFAULT_MAX_TOTAL_CONNECTIONS = 
		Integer.parseInt(confProperties.getProperty("crawler.default.max.total.connections", "20").trim());
	
	/*
	 * configurations for webPageFetcher
	 */
	public static final int CRAWLER_PAGE_FETCH_THREAD_NUM = 
		Integer.parseInt(confProperties.getProperty("crawler.host.pageFetchThread.num", "1").trim());
	
	public static final int CRAWLER_CONNECT_TIMEOUT = 
		Integer.parseInt(confProperties.getProperty("crawler.host.connect.timeout", "30").trim());//30 seconds
	
	public static final int CRAWLER_READ_TIMEOUT = 
		Integer.parseInt(confProperties.getProperty("crawler.host.read.timeout", "30").trim());//30 seconds
	
	// about 1M
	public static final int CRAWLER_MAX_CONTENT_LENGTH = 
		Integer.parseInt(confProperties.getProperty("crawler.host.max.content.length", "1024000").trim());
	
	public static final String CRAWLER_WEB_DEFAULT_CHARSET = 
		confProperties.getProperty("crawler.host.web.default.charset", "GB18030").trim();
	
	/*
	 * ##### configurations for crawler manager######
	 */
	
	public static int CRAWLER_NUMBER_ONE_COMPUTER = 
		Integer.parseInt(confProperties.getProperty("crawler.number.one.computer", "4").trim());
	
	public static final int CRAWLER_FINISH_CHECK_WAITTING_MILLISECONDS = 
		Integer.parseInt(confProperties.getProperty("crawler.finish.check.waitting.milliseconds", "600000").trim());
	
	public static boolean CRAWLER_SEND = 
		Boolean.parseBoolean(confProperties.getProperty("crawler.send", "false").trim());
	
	public static boolean CRAWLER_STORE = 
		Boolean.parseBoolean(confProperties.getProperty("crawler.store", "false").trim());	
	
	public static int CRAWLER_SEND_RAWPAGE_INTERVAL = 
		Integer.parseInt(confProperties.getProperty("crawler.send.rawpage.interval", "0").trim());
	
	public static int CRAWLER_SEND_RAWPAGE_ONCE_PAGENUMBER = 
		Integer.parseInt(confProperties.getProperty("crawler.send.rawpage.once.pagenumber", "20").trim());
	

	
	/*
	 * ##### configurations for page prepare processor######
	 */
	public static final int CRAWLER_CLEAN_QUEUE_LENGTH_MIN = 
		Integer.parseInt(confProperties.getProperty("crawler.clean.queue.length.min", "500").trim());
	
	public static final int CRAWLER_CLEAN_QUEUE_LENGTH_MAX = 
		Integer.parseInt(confProperties.getProperty("crawler.clean.queue.length.max", "1000").trim());
	
	
	/*
	 * configurations for timer
	 */
	public static int TIME_WEB_TASK_CRAWL_HOUR = 
		Integer.parseInt(confProperties.getProperty("time.web.task.crawl.hour", "0").trim());
	
	public static int TIME_WEB_TASK_CRAWL_MINUTE = 
		Integer.parseInt(confProperties.getProperty("time.web.task.crawl.minute", "0").trim());
	
	public static int TIME_BLOG_TASK_CRAWL_HOUR = 
		Integer.parseInt(confProperties.getProperty("time.blog.task.crawl.hour", "4").trim());
	
	public static int TIME_BLOG_TASK_CRAWL_MINUTE = 
		Integer.parseInt(confProperties.getProperty("time.blog.task.crawl.minute", "0").trim());
	
	public static int TIME_BBS_TASK_CRAWL_HOUR = 
		Integer.parseInt(confProperties.getProperty("time.bbs.task.crawl.hour", "0").trim());
	
	public static int TIME_BBS_TASK_CRAWL_MINUTE = 
		Integer.parseInt(confProperties.getProperty("time.bbs.task.crawl.minute", "20").trim());
	
	public static int INTERVAL_CRAWL_WEB_TASK = 
		Integer.parseInt(confProperties.getProperty("interval.crawl.web.task", "4").trim());
	
	public static int INTERVAL_CRAWL_BLOG_TASK = 
		Integer.parseInt(confProperties.getProperty("interval.crawl.blog.task", "0").trim());
	
	public static int INTERVAL_CRAWL_BBS_TASK = 
		Integer.parseInt(confProperties.getProperty("interval.crawl.bbs.task", "0").trim());
	
	public static int INTERVAL_UPDATE_CONF_TASK = 
		Integer.parseInt(confProperties.getProperty("interval.update.conf.task", "0").trim());
	
	public static boolean MASTER_PLOT_OPEN = 
		Boolean.parseBoolean(confProperties.getProperty("master.plot.open", "false").trim());

	public static final String MASTER_PLOT_EXECUTEFILEPATH = 
		confProperties.getProperty("master.plot.executefilepath", "lib/gnuplot/bin/pgnuplot.exe").trim();
	
	public static int MASTER_PLOT_YRANGE = 
		Integer.parseInt(confProperties.getProperty("master.plot.yrange", "0").trim());
	
	public static int INTERVAL_BBS_UPDATE_FETCHOR = 
		Integer.parseInt(confProperties.getProperty("interval.bbs.update.fetchor", "0").trim());
	
	public static void reloadConf(){
		System.out.println("conf reloading");
		confProperties = Utilities.loadProperties("conf/conf.properties");
		/*
		 * ##### configurations for crawler manager######
		 */
		CRAWLER_NUMBER_ONE_COMPUTER = Integer.parseInt(confProperties.getProperty("crawler.number.one.computer", "4").trim());
		CRAWLER_SEND = Boolean.parseBoolean(confProperties.getProperty("crawler.send", "false").trim());
		CRAWLER_STORE = Boolean.parseBoolean(confProperties.getProperty("crawler.store", "false").trim());	
		CRAWLER_SEND_RAWPAGE_INTERVAL = Integer.parseInt(confProperties.getProperty("crawler.send.rawpage.interval", "0").trim());
		CRAWLER_SEND_RAWPAGE_ONCE_PAGENUMBER = Integer.parseInt(confProperties.getProperty("crawler.send.rawpage.once.pagenumber", "20").trim());
		/*
		 * configurations for timer
		 */
		TIME_WEB_TASK_CRAWL_HOUR = Integer.parseInt(confProperties.getProperty("time.web.task.crawl.hour", "0").trim());
		TIME_WEB_TASK_CRAWL_MINUTE = Integer.parseInt(confProperties.getProperty("time.web.task.crawl.minute", "0").trim());
		TIME_BLOG_TASK_CRAWL_HOUR = Integer.parseInt(confProperties.getProperty("time.blog.task.crawl.hour", "4").trim());
		TIME_BLOG_TASK_CRAWL_MINUTE = Integer.parseInt(confProperties.getProperty("time.blog.task.crawl.minute", "0").trim());
		TIME_BBS_TASK_CRAWL_HOUR = Integer.parseInt(confProperties.getProperty("time.bbs.task.crawl.hour", "0").trim());
		TIME_BBS_TASK_CRAWL_MINUTE = Integer.parseInt(confProperties.getProperty("time.bbs.task.crawl.minute", "20").trim());
		INTERVAL_CRAWL_WEB_TASK = Integer.parseInt(confProperties.getProperty("interval.crawl.web.task", "4").trim());
		INTERVAL_CRAWL_BLOG_TASK = Integer.parseInt(confProperties.getProperty("interval.crawl.blog.task", "0").trim());
		INTERVAL_CRAWL_BBS_TASK = Integer.parseInt(confProperties.getProperty("interval.crawl.bbs.task", "0").trim());
		INTERVAL_UPDATE_CONF_TASK = Integer.parseInt(confProperties.getProperty("interval.update.conf.task", "0").trim());
	}
}
